import { login, getUserInfo } from '../../api/api'

export default {
    namespaced: true, // 命名空间
    state: () => ({
        token: window.localStorage.vuex ? JSON.parse(window.localStorage.vuex).user.token : '',
        uid: window.localStorage.vuex ? JSON.parse(window.localStorage.vuex).user.uid : '',
    }),
    mutations: {
        changeToken(state, payload) {
            // 存储到仓库
            state.token = payload
        },
        changeuid(state, payload) {
            // 存储到仓库
            state.uid = payload
        }
    },
    actions: {
        async getToken({ commit, state }, params) {
            let res = await login(params)
            if (res.data.status == 200) {
                // 更改状态
                commit('changeToken', res.data.data.token)
            }
        },
        async getUser({ commit, state }, params) {
            let res = await getUserInfo()
            if (res.data.status == 200) {
                commit('changeuid', res.data.data.uid)
            }
        }
    }
}